//
//  SearchBarViewController.m
//  逗逼电影
//
//  Created by 王义凯 on 14-10-10.
//  Copyright (c) 2014年 wangyikai. All rights reserved.
//

#import "SearchBarViewController.h"
#import "MBProgressHUD.h"
#import "NetHandler.h"
#import "Weather.h"
#import "weatherViewController.h"
@interface SearchBarViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)UISearchBar *searchBar;
@property (nonatomic ,retain)UITableView *tableView;
@property (nonatomic ,retain)NSMutableArray *showArray;
@property (nonatomic, retain)Weather *weather;

@end

@implementation SearchBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
        self.searchArray = [NSMutableArray array];
        self.showArray = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc
{
    [super dealloc];
    [_searchArray release];
    [_searchBar release];
    [_tableView release];
    [_showArray release];
    [_weather release];
    [_array release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -20, 320, 50)];
    self.searchBar.placeholder=@"请输入您要查询的城市";
    self.searchBar.delegate = self;
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.showsCancelButton = YES;
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeDefault;
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 548) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.searchBar;
    [self.view addSubview:_tableView];
    [_searchBar release];
    [_tableView release];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"首页" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonClicked:)];
}
#pragma mark button的点击方法
- (void)buttonClicked:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self.showArray removeAllObjects];
    //NSLog(@"%@",searchText);
    for (NSString *str in self.searchArray) {
        NSRange range = [str rangeOfString:searchText];
        if (range.length != 0) {
            [self.showArray addObject:str];
        }
        [self.tableView reloadData];
    }
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.showArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = @"http://iphone.myzaker.com/zaker/get_weather.php?city=";
    NSString *strWeather = [str stringByAppendingFormat:@"%@",self.showArray[indexPath.row]];
    [NetHandler networkWithURL:strWeather comaletion:^(id result) {
        NSDictionary *weather = [[result objectForKey:@"data"]objectForKey:@"datas"];
        
        self.weather = [[Weather alloc] init];
        self.weather.tmax = [weather objectForKey:@"tmax"];
        self.weather.tmin = [weather objectForKey:@"tmin"];
        self.weather.weather_day = [weather objectForKey:@"weather_day"];
        self.weather.weather_night = [weather objectForKey:@"weather_night"];
        self.weather.title = self.showArray[indexPath.row];
        
        if (self.searchBar.text ==  nil) {
            
        } else {
            NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
            
            [noti postNotificationName:@"weatherChange" object:self.weather];
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]autorelease];
    }
    cell.textLabel.text = [self.showArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
